﻿//.入口参数
var topCatForm = document.aspnetForm.topCatFormKey;
var secondCatForm = document.aspnetForm.secondCatFormKey;
var leafCatForm = document.aspnetForm.leafCatFormKey;
//下拉列表的起始索引
var beginIndex = 0;

//top category methods
function TopCategory(id,title) {
    this.id = id;
    this.title = title;
    this.childCategorys = new Array();
    this.childOptions = new Array();
    this.addChildCategory = addChildCategory;
    this.option = new Option(this.title,this.id);
    this.getChildOptions = getChildOptions;
}
//category methods
function CA(parent,id,title) {
    this.parent = parent;
    this.id = id;
    this.title = title;
    this.childCategorys = new Array();
    this.childOptions = new Array();
    this.addChildCategory = addChildCategory;

    this.option = new Option(this.title,this.id);
    this.getChildOptions = getChildOptions;
    parent.addChildCategory(this);
}

//leafage category methods
function L(parent,id,title) {
    this.parent = parent;
    this.id = id;
    this.title = title;
    this.option = new Option(this.title,this.id);
    parent.addChildCategory(this);
}

function addChildCategory(category) {
    this.childCategorys = this.childCategorys.concat(category);
    this.childOptions = this.childOptions.concat(category.option);
}

function getChildOptions() {
    return this.childOptions;
}


function changeTopCategory() {
	secondCatForm.value = "";
    if (topCatForm.selectedIndex == -1 || (topCatForm.selectedIndex == 0 && beginIndex == 1)) {
        if (beginIndex == 1)
            topCatForm.options[0].selected = true;
        secondOptions = secondCatForm.options;
        var len = secondCatForm.options.length;
        for (var i = len - 1; i > beginIndex - 1; i--) {
            secondCatForm.options[i] = null;
        }
    }
    else {
	    var selectedTopCategory;
		for(var j =0;j<topCatArr.length;j++){
			if(topCatForm.options[topCatForm.selectedIndex].value == topCatArr[j].id){
				selectedTopCategory = topCatArr[j];
			} 
		}
        var secondOptions = selectedTopCategory.getChildOptions();
        var len = secondCatForm.options.length;
        for (var i = len - 1; i > beginIndex - 1; i--) {
            secondCatForm.options[i] = null;
        }
        for (var i = 0; i < secondOptions.length; i++) {
            secondCatForm.options[i + beginIndex] = secondOptions[i];
        }
    }
    if (beginIndex == 1)
        secondCatForm.options[0].selected = true;
    changeSecondCategory();
}

function changeSecondCategory() {
	leafCatForm.value = "";
    if (secondCatForm.selectedIndex == -1 || (secondCatForm.selectedIndex == 0 && beginIndex == 1)) {
        if (beginIndex == 1)
            secondCatForm.options[0].selected = true;
        var len = leafCatForm.options.length;
        for (var i = len - 1; i > beginIndex - 1; i--) {
            leafCatForm.options[i] = null;
        }
    }
    else {
        var option = secondCatForm.options[secondCatForm.selectedIndex];
        var thirdOptions = getSecondCategoryById(option.value).getChildOptions();
        var len = leafCatForm.options.length;
        for (var i = len - 1; i > beginIndex - 1; i--) {
            leafCatForm.options[i] = null;
        }
        for (var i = 0; i < thirdOptions.length; i++) {
            leafCatForm.options[i + beginIndex] = thirdOptions[i];
        }
    }
    if (beginIndex == 1)
        leafCatForm.options[0].selected = true;
}

function getSecondCategoryById(id) {
    for (var i = 0; i < topCatArr.length; i++) {
        var childCategorys = topCatArr[i].childCategorys;
        var len = childCategorys.length;
        for (var j = 0; j < len; j++) {
            if (childCategorys[j].id == id) {
                return childCategorys[j];
            }
        }
    }
}
 

function initTopCategoryForm() {
    var size = topCatArr.length;
    for (var i = 0; i < size; i++) {
        topCatForm.options[i + beginIndex] = topCatArr[i].option;
    }
    changeTopCategory();
}

var topCatArr = new Array();
var T;
var S;

